//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#include <ddk.h>

#include "ddkit.h"
#include "usbapi.h"
#include "proxy.h"
#include "bulk.h"
#include "usbdbg.h"

CBulk g_Bulk;

ECode UmCtrl::Read(
        /* [in] */ Int64 u64Offset,
        /* [in] */ Int32 bytesToRead,
        /* [out] */ MemoryBuf * pBuffer,
        /* [out] */ IEvent * * ppCompletionEvent)
{
    return E_NOT_IMPLEMENTED;
}


ECode UmCtrl::Write(
        /* [in] */ Int64 u64Offset,
        /* [in] */ const MemoryBuf & buffer,
        /* [out] */ Int32 * pBytesWritten,
        /* [out] */ IEvent * * ppCompletionEvent)
{
    return E_NOT_IMPLEMENTED;
}


ECode UmCtrl::Control(
        /* [in] */ Handle32 nControlCode,
        /* [in] */ const MemoryBuf & inBuffer,
        /* [out] */ MemoryBuf * pOutBuffer,
        /* [out] */ IEvent * * ppCompletionEvent)
{
    Int32 OpCode = nControlCode;
    Boolean ClsBlock = FALSE;
    ECode ec = NOERROR;
    wchar_t *pVol = NULL;
    Int32 Size = 0;

    switch(OpCode) {
        case MASS_INIT://Protocol Initialize, Do not Expose to User, use for self test...
/*Temporarily Shut off
            pVol = CONV(wchar_t*, inBuffer);
            Size = inBuffer.GetUsed();
            if (Size > 0) {
                DPRINTFN(DBG_INIT, ("USB Mass-Storage Initialize...\n"));
                ec = g_Bulk.Bulk_Init(pVol);
                if (FAILED(ec)) {
                    g_Bulk.Bulk_DeInit();
                }
                else {
                    kputs("USB Host Controller Enumerate...\n");
                }
            }
            else {
                ec = E_INVALID_ARGUMENT;
            }
*/
            break;

        case MASS_STOP_BLKCLS:
            ClsBlock = TRUE;
        case MASS_STOP:
            g_Bulk.Bulk_Stop(ClsBlock);
            break;

        case MASS_WAKE_BLKOPN:
            pVol = CONV(wchar_t*, inBuffer.GetPayload());
            Size = inBuffer.GetUsed();
            if (Size == 0) {
                pVol = NULL;
            }
        case MASS_WAKE:
            ec = g_Bulk.Bulk_Wake(pVol);
            if (FAILED(ec)) {
                g_Bulk.Bulk_DeInit();
            }
            else {
                cputs("USB Host Controller Enumerate...\n");
            }
            break;

        default:
            DPRINTFN(1, ("UnKnown Op Code %08x\n", OpCode));
            ec = E_INVALID_ARGUMENT;
    }

    return ec;
}

void UmCtrl::Dispose()
{
}

UmCtrl g_UmCtrl;
extern CUsbApi g_UsbApi;
EXTERN IDeviceDriver * CDECL CreateUsbMass(uint_t uDeviceNo, void *pvParameter)
{
    cputs("Register USB Mass-Storage\n");
    g_Bulk.m_IsInit = FALSE;
    g_Bulk.m_pUfunc = &g_UsbApi;
    return (&g_UmCtrl);
}

